{% extends 'admin/common/base.html' %}
{% block body %}

<body class="gray-bg">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="role-form">
                    <div class="select-list">
                        <ul>
                            <li>
                                名称：<input type="text" name="name" />
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
                    <i class="fa fa-remove"></i> 删除
                </a>
            </div>

            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
</body>
{% endblock %}
{% block js %}
{{ super() }}
<script src="{{url_for('admin.static',filename='ajax/libs/highlight/highlight.min.js')}}"></script>
<script>

    $(function () {
        var options = {
            url: "{{url_for('admin.sys.gencode.list')}}",
            createUrl: "{{url_for('admin.sys.gencode.add')}}",
            updateUrl: "{{url_for('admin.sys.gencode.edit')}}?id={id}",
            removeUrl: "{{url_for('admin.sys.gencode.remove')}}?id={id}",
            sortName: "roleSort",
            modalName: "代码生成",
            columns: [{
                checkbox: true
            },
            {
                field: 'id',
                title: '编号'
            },
            {
                field: 'name',
                title: '名称',
                sortable: true
            },
            {
                field: 'model',
                title: '模块名称',
                sortable: true
            },
            {
                field: 'ctime',
                title: '创建时间',
                sortable: true
            },
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var actions = [];
                    actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" onclick="preview(\'' + row.id + '\')"><i class="fa fa-search"></i>预览</a> ');
                    actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                    actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                    actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="genCode(\'' + row.id + '\', \'' + row.name + '\')"><i class="fa fa-bug"></i>生成代码</a> ');
                    return actions.join('');
                }
            }]
        };
        $.table.init(options);
    });


    // 预览代码
    function preview(id) {
        var preViewUrl = "{{url_for('admin.sys.gencode.preview')}}?id=" + id;
        $.modal.loading("正在加载数据，请稍后...");
        $.get(preViewUrl, function (result) {
            if (result.code == web_status.SUCCESS) {
                var items = [];
                $.each(result.data, function (index, value) {
                    var templateName = value.name;
                    if (!$.common.equals("sql", templateName) && !$.common.equals("tree.html", templateName) && !$.common.equals("sub-domain.java", templateName)) {
                        var language = templateName.substring(templateName.lastIndexOf(".") + 1);
                        var highCode = hljs.highlight(language, value.content).value;
                        items.push({
                            title: templateName, content: "<pre class=\"layui-code\"><code>" + highCode + "</code></pre>"
                        })
                    }
                });
                top.layer.tab({
                    area: ['90%', '90%'],
                    shadeClose: true,
                    success: function (layero, index) {
                        parent.loadCss("{{url_for('admin.static',filename='ajax/libs/highlight/default.min.css')}}");
                    },
                    tab: items
                });
            } else {
                $.modal.alertError(result.msg);
            }
            $.modal.closeLoading();
        });
    }

    // 生成代码
		function genCode(id, name) {
		    $.modal.confirm("确定要生成" + name + "表代码吗？", function() {
				$.operate.get("{{url_for('admin.sys.gencode.gencode')}}?id=" + id);
		    })
		}

</script>
{% endblock %}